package com.lsm.czxxgl_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsm.czxxgl_system.constant.UserConstant;
import com.lsm.czxxgl_system.model.domain.User;
import com.lsm.czxxgl_system.model.request.UserSearchRequest;
import com.lsm.czxxgl_system.service.UserService;
import com.lsm.czxxgl_system.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author shu
 * @description 针对表【user(单位)】的数据库操作Service实现
 * @createDate 2024-06-25 10:46:34
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    private static final String SALT = "666";

    @Override
    public User userLogin(String username, String password, HttpServletRequest httpServletRequest) {
        //判断用户名和密码是否为空
        if (StringUtils.isAnyBlank(username, password)) {
            return null;
        }
        //查询用户名和密码是否正确
        String encryptPassword = DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8));
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", username);
        userQueryWrapper.eq("user_password", encryptPassword);
        User user = this.getOne(userQueryWrapper);
        if (user == null) {
            //用户名或密码错误
            return null;
        }
        //用户脱敏
        user = this.getSafetyUser(user);
        //保存用户登录态
        httpServletRequest.getSession().setAttribute(UserConstant.USER_LOGIN_STATE, user);
        return user;
    }

    @Override
    public User getSafetyUser(User user) {
        if (user == null) {
            return null;
        }
        User safetyUser = new User();
        safetyUser.setId(user.getId());
        safetyUser.setUserEncode(user.getUserEncode());
        safetyUser.setUnit(user.getUnit());
        safetyUser.setUsername(user.getUsername());
        safetyUser.setUserRole(user.getUserRole());
        safetyUser.setAddition(user.getAddition());
        return safetyUser;
    }

    @Override
    public Integer saveUser(User user) {
        //判断用户信息是否合法
        if (StringUtils.isAnyBlank(user.getUserEncode(), user.getUsername(), user.getUserPassword())) {
            return null;
        }
        //进行密码的加密
        user.setUserPassword(DigestUtils.md5DigestAsHex(user.getUserPassword().getBytes(StandardCharsets.UTF_8)));
        this.save(user);

        return user.getId();
    }

    @Override
    public boolean updateUser(User user) {
        //判断用户信息是否合法
        if (StringUtils.isAnyBlank(user.getUserEncode(), user.getUsername())) {
            return false;
        }
        return this.updateById(user);
    }

    @Override
    public List<User> searchByCondition(UserSearchRequest userSearchRequest) {
        String userEncode = userSearchRequest.getUserEncode();
        String username = userSearchRequest.getUsername();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        //根据查询条件设置queryWrapper对象
        if (!StringUtils.isBlank(userEncode)) {
            userQueryWrapper.like("user_encode",userEncode);
        }
        if (!StringUtils.isBlank(username)) {
            userQueryWrapper.like("username",username);
        }
        //根据条件查询数据库
        List<User> list = this.list(userQueryWrapper);
        return list;
    }


}




